/**
 ******************************************************************************
 * @file    Project/STM32F4xx_StdPeriph_Templates/stm32f4xx_it.c
 * @author  MCD Application Team
 * @version V1.5.0
 * @date    06-March-2015
 * @brief   Main Interrupt Service Routines.
 *          This file provides template for all exceptions handler and
 *          peripherals interrupt service routine.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
 *
 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *        http://www.st.com/software_license_agreement_liberty_v2
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_it.h"
#include "app_timer.h"
#include "stm32f4x7_eth.h"
#include "system_led.h"

/* Scheduler includes */
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
/* lwip includes */
#include "device_resource.h"
#include "lwip/sys.h"
#include "module_rs485_driver.h"
#include "own_chip_rtc.h"
#include "print_rtt.h"
#include "rs485_port_init.h"
#include "rtc_func.h"
/** @addtogroup Template_Project
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M4 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
 * @brief  This function handles NMI exception.
 * @param  None
 * @retval None
 */
void NMI_Handler(void)
{
}

/**
 * @brief  This function handles Hard Fault exception.
 * @param  None
 * @retval None
 */
void HardFault_Handler(void)
{
  //	printf("hardfault!!!\n");
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
 * @brief  This function handles Memory Manage exception.
 * @param  None
 * @retval None
 */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
 * @brief  This function handles Bus Fault exception.
 * @param  None
 * @retval None
 */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
 * @brief  This function handles Usage Fault exception.
 * @param  None
 * @retval None
 */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

///**
// * @brief  This function handles SVCall exception.
// * @param  None
// * @retval None
// */
// void SVC_Handler(void)
//{
//}

/**
 * @brief  This function handles Debug Monitor exception.
 * @param  None
 * @retval None
 */
void DebugMon_Handler(void)
{
}

void UART5_IRQHandler(void)
{
  rs485_private_rtx_isr(&moudule_rs485);
}
// TIM6中断服务程序
void TIM6_DAC_IRQHandler(void)
{
  rs485_private_rx_tim_isr(&moudule_rs485);
}
void TIM1_UP_TIM10_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
    ++device_time_ms_tickets;
    if (device_time_ms_tickets > (4294967295000))
    {
      device_time_ms_tickets = 0;
    }
  }
}
///**
// * @brief  This function handles PendSVC exception.
// * @param  None
// * @retval None
// */
// void PendSV_Handler(void)
//{
//}

/**
 * @brief  This function handles SysTick Handler.
 * @param  None
 * @retval None
 */
// void SysTick_Handler(void)
//{
//   App_SysTick_Handler();
// }
/**
 * @brief  TIM8更新中断服务函数
 * @param  无
 * @retval 无
 */
void TIM8_UP_TIM13_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM8, TIM_IT_Update);
    system_led_toggle();
  }
}
// 以太网中断
/**
 * @brief  This function handles ETH interrupt request.
 * @param  None
 * @retval None
 */
void ETH_IRQHandler(void)
{
  portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  extern SemaphoreHandle_t s_xSemaphore;
  /* Frame received */
  if (ETH_GetDMAFlagStatus(ETH_DMA_FLAG_R) == SET)
  {
    /* Give the semaphore to wakeup LwIP task */
    xSemaphoreGiveFromISR(s_xSemaphore, &xHigherPriorityTaskWoken);
  }

  /* Clear the interrupt flags. */
  /* Clear the Eth DMA Rx IT pending bits */
  ETH_DMAClearITPendingBit(ETH_DMA_IT_R);
  ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);

  /* Switch tasks if necessary. */
  if (xHigherPriorityTaskWoken != pdFALSE)
  {
    portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
  }
}

void RTC_WKUP_IRQHandler(void)
{
  if (RTC_GetITStatus(RTC_IT_WUT) != RESET)
  {
    static struct tm tm;
    uint8_t y, mo, d, w, h, m, s;

    RTC_ClearITPendingBit(RTC_IT_WUT);
    RTC_ClearFlag(RTC_FLAG_WUTF);
    EXTI_ClearITPendingBit(EXTI_Line22);

    OWN_CHIP_RTC_GetDate(&y, &mo, &d, &w);
    OWN_CHIP_RTC_GetTime(&h, &m, &s);
    rtc_vals_to_tm(y, mo, d, h, m, s, w, &tm);
    device_time_tickets = mktime(&tm); // 若 HardFault 先注释它
    device_time_ms_tickets = ((uint64_t)device_time_tickets) * 1000ULL;
    LOG_COLOR(YELLOW, "RTC Wakeup: %04d-%02d-%02d %02d:%02d:%02d\n", y, mo, d, h, m, s);
  }
}
/**
 * @brief EXTI9_5中断服务函数：自动完成“标志清除+数值重载”，实现连续中断
 * 文档依据：8.3.2节（清除TF标志）、8.8.2节（重装定时器数值）
 */
void EXTI9_5_IRQHandler(void)
{
  volatile uint8_t temp;
  if (EXTI_GetITStatus(EXTI_Line7) != RESET)
  {
    // 1. 用户自定义：每分钟中断任务（如LED翻转、数据记录等）
    // ...
    LOG_COLOR(YELLOW, "PCF8563 Wakeup\n");
    RTC_Chip_Timer_int_handler();
    // 4. 清除STM32 EXTI7中断标志
    EXTI_ClearITPendingBit(EXTI_Line7);
  }
}
/******************************************************************************/
/*                 STM32F4xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f4xx.s).                                               */
/******************************************************************************/

/**
 * @brief  This function handles PPP interrupt request.
 * @param  None
 * @retval None
 */
/*void PPP_IRQHandler(void)
{
}*/

/**
 * @}
 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
